﻿using SharpDepend.XamarinForms.Controls;
using System;
using Xamarin.Forms;

namespace SharpDepend.XamarinForms
{

    public class DialogManager : IDialogManager
    {
        internal Page _NavigationPage;

        public DialogManager(Page navigationPage)
        {
            _NavigationPage = navigationPage;
        }

        public override void Show(string message, string title, DialogButtons dialogButtons, DialogIcon dialogIcon, DialogResultCallback result, object parameter)
        {
            ShowDialog(message, title, dialogButtons, dialogIcon, result, parameter);
        }

#if WP7
        private void ShowDialog(string message, string title, DialogButtons dialogButtons, DialogIcon dialogIcon, DialogResultCallback result, object parameter)
        {
        }
#else
        private async void ShowDialog(string message, string title, DialogButtons dialogButtons, DialogIcon dialogIcon, DialogResultCallback result, object parameter)
        {
            try
            {
                switch (dialogButtons)
                {
                    case DialogButtons.OK:
                        {
                            var res = _NavigationPage.DisplayAlert(title, message, "OK");
                        }
                        break;
                    case DialogButtons.YesNo:
                        {
                            var res = await _NavigationPage.DisplayAlert(title, message, "Yes", "No");

                            if (result != null)
                            {
                                result(res ? DialogButtonResult.Yes : DialogButtonResult.No, parameter);
                            }
                        }
                        break;
                    default:
                        throw new NotSupportedException();
                }
            }
            catch (Exception)
            {

            }
        }
#endif

        public override void Input(string label, string title, string defaultInput, DialogInputDialoigCallback result, object parameter)
        {
            ContentPage page = new ContentPage();
            InputBox dialog = new InputBox(label, defaultInput);
            dialog.OnInputActionDone += delegate (string obj)
            {
                // TODO: If the file do not exist?
                result(true, obj, parameter);

                _NavigationPage.Navigation.PopAsync();
            };
            page.Title = title;
            page.Content = dialog;
            _NavigationPage.Navigation.PushAsync(page);
        }

        public override void OpenFile(string title, string extensions, string defaultPath, DialogOpenSaveFileDialoigCallback result, object parameter)
        {
            throw new NotImplementedException();
            //ContentPage page = new ContentPage();
            //OpenFileDialog dialog = new OpenFileDialog(title, extensions, defaultPath, SharpDepend.Manager.FileManager);
            //dialog.OnFileActionDone += delegate (string obj)
            //{
            //    // TODO: If the file do not exist?
            //    result(true, obj, parameter);

            //    _NavigationPage.Navigation.PopAsync();
            //};
            //page.Title = dialog.Title;
            //page.Content = dialog;
            //_NavigationPage.Navigation.PushAsync(page);
        }

        public override void SaveFile(string title, string extensions, string defaultPath, DialogOpenSaveFileDialoigCallback result, object parameter)
        {
            throw new NotImplementedException();
            //ContentPage page = new ContentPage();
            //SaveFileDialog dialog = new SaveFileDialog(title, extensions, defaultPath, SharpDepend.Manager.FileManager);
            //dialog.OnFileActionDone += delegate (string obj)
            //{
            //    // TODO: If the file do not exist?
            //    result(true, obj, parameter);

            //    _NavigationPage.Navigation.PopAsync();
            //};
            //page.Title = dialog.Title;
            //page.Content = dialog;
            //_NavigationPage.Navigation.PushAsync(page);
        }

        public override void OpenFolder(string title, string defaultPath, DialogOpenSaveFileDialoigCallback result, object parameter)
        {
            throw new NotImplementedException();
            //OpenFolderDialog dialog = new OpenFolderDialog(title, defaultPath, SharpDepend.Manager.FileManager);
            //dialog.OnFileActionDone += delegate (string obj)
            //{
            //    // TODO: If the file do not exist?
            //    result(true, obj, parameter);

            //    _NavigationPage.Navigation.PopAsync();
            //};
            //_NavigationPage.Navigation.PushAsync(dialog);
        }
    }
}
